///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2009. Dimarik
//
///////////////////////////////////////////////////////////////////////////////

package
{

import com.bit101.components.CheckBox;
import com.bit101.components.ComboBox;
import com.bit101.components.InputText;
import com.bit101.components.Label;
import com.bit101.components.ProgressBar;
import com.bit101.components.PushButton;

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.getTimer;

import ru.goodcore.display.GIFLoader;
//import ru.goodcore.utils.swf.SWFGIFMovieClip;

/**
 *  @author				Dimarik
 *  @version			1.0
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="1024", height="768", frameRate="21", backgroundColor="#E0EEEE")]

public class GIFMovieClipTest1 extends Sprite {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function GIFMovieClipTest1() {
		super();
		this.init();
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 */
	private var _loader:		Loader;

	/**
	 * @private 
	 */
	private var _qname:			QName;

	/**
	 * @private 
	 */
	private var _mc:			MovieClip;

	/**
	 * @private 
	 */
	private var _comboBox:		ComboBox;
	private var _comboBoxIndex:	uint;

	/**
	 * @private 
	 */
	private var _checkBox:		CheckBox;

	/**
	 * @private 
	 */
	private var _inputText:		InputText;

	/**
	 * @private 
	 */
	private var _progressBar:	ProgressBar;

	/**
	 * @private 
	 */
	private var _label:			Label;

	/**
	 * @private 
	 */
	private var _useDelays:		Boolean = true;

	/**
	 * @private 
	 */
	private var _start:			Number;

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	public function init():void {
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;

		var uris:Array = [
			'http://gifzona.com/i/mult/05.gif',
			'http://gifzona.com/i/priroda/1111.gif',
			'http://livegif.ru/Gallery/BEST/fox1.gif',
			'http://pimg.dt00.net/pnews/flashki.ru/681770_b.gif',
			'http://www.gas13.ru/v3/pixelart/wall.gif',
			'http://www.gas13.ru/v3/tutorials/11-5.gif',
			'http://www.gas13.ru/v3/tutorials/8-16.gif',
			'http://www.gifandgif.eu/animated_gif/Arrows/Animated%20Gif%20Arrows%20(104).gif',
			'http://www.livegif.ru/Gallery/ZVERI/BIRDS/BIRDHUML.gif',
			'http://www.pixeljoint.com/files/icons/fool.gif',
			'http://www.pixeljoint.com/files/icons/full/4.gif',
			'http://www.pixeljoint.com/files/icons/full/assault_prototype.gif',
			'http://www.pixeljoint.com/files/icons/full/avat.gif',
			'http://www.pixeljoint.com/files/icons/full/dog.gif',
			'http://www.pixeljoint.com/files/icons/full/food.gif',
			'http://www.pixeljoint.com/files/icons/full/newmario.gif',
			'http://www.pixeljoint.com/files/icons/full/pcdesign_final.gif',
			'http://www.pixeljoint.com/files/icons/full/shoe.gif',
			'http://www.pixeljoint.com/files/icons/honey_run_arm00.gif',
			'http://www.pixeljoint.com/files/icons/px_spear.gif',
			'http://gifdr.narod.ru/i/dr/03.gif',
			'http://gifdr.narod.ru/i/dr/10.gif',
			'http://www.princeton.edu/~rvdb/JAVA/election2004/TwoMaps.gif',
			'http://www.solarviews.com/raw/jup/ganymed3.gif',
			'http://www.pixeljoint.com/files/icons/full/completed.gif'
		];
		
		this._checkBox = new CheckBox(super.stage, 500, 35, 'use gif delays', this.handler_delayChange);
		this._checkBox.selected = true;

		this._label = new Label(super.stage, 500, 10, 'Ready');

		var progressBar:ProgressBar = new ProgressBar();

		progressBar.x = (super.stage.stageWidth - progressBar.width) >> 1;
		progressBar.y = (super.stage.stageHeight - progressBar.height) >> 1;
		this._progressBar = progressBar;

		var button:PushButton = new PushButton(super.stage, 310, 40, 'Add to list', this.handler_pressButton);
		button.enabled = false;

		this._inputText = new InputText(super.stage, 10, 42, 'or type custom URL here...');
		this._inputText.addEventListener(FocusEvent.FOCUS_IN, 
			function(event:Event):void {
				var inp:InputText = InputText(event.currentTarget); 
				inp.removeEventListener(event.type, arguments.callee);
				inp.text = '';
				button.enabled = true;
			}
		);

		this._inputText.width = 290;


		var comboBox:ComboBox = new ComboBox(super.stage, 10, 10, 'choose ULR', uris);

		this._comboBox = comboBox;

		comboBox.width = 400;
		comboBox.addEventListener(Event.SELECT, this.handler_select);

		for (var i:uint = 0; i < uris.length; i++) {
			comboBox.addItem( {label:uris[i]} );
			this._comboBoxIndex++;
		}

		this._qname = new QName('ru.goodcore', 'GIFMovieClip');
	}

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	private function handler_select(event:Event = null):void {
		
		if (this._comboBox.selectedItem == null)
			return;

		if (this._mc && this._mc.parent)
			this._mc.parent.removeChild(this._mc);

		this._label.text = 'Loading...';

		this._comboBox.removeEventListener(Event.SELECT, this.handler_select);
		
		super.stage.addChild(this._progressBar);
 
		this._progressBar.x = (super.stage.stageWidth - this._progressBar.width) >> 1;
		this._progressBar.y = (super.stage.stageHeight - this._progressBar.height) >> 1;

		var uri:String = ComboBox(event.target).selectedItem.label;

		var request:URLRequest = new URLRequest(uri);

		this._loader = new GIFLoader(this._qname, true, this._useDelays, super.stage.frameRate);

		this._loader.addEventListener(ProgressEvent.PROGRESS,	this.handler_progress);
		this._loader.addEventListener(IOErrorEvent.IO_ERROR,	this.handler_error);

		this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.handler_resourceComplete);
		this._loader.load(request);		
	}

	/**
	 * @private
	 */
	private function handler_delayChange(event:Event):void {
		var checkBox:CheckBox = CheckBox(event.target);
		this._useDelays = checkBox.selected;
		if (this._comboBox.selectedItem) {
			this._comboBox.dispatchEvent( new Event(Event.SELECT) );
		}
	}

	/**
	 * @private
	 */
	private function handler_pressButton(event:Event):void {
		this._comboBox.removeEventListener(Event.SELECT, this.handler_select);

		if (this._inputText.text == '')
			return;

		var comboBox:	ComboBox = this._comboBox;

		for (var i:uint = 0; i < this._comboBoxIndex; i++) {
			comboBox.selectedIndex = i;
			if (comboBox.selectedItem.label == this._inputText.text)
				return;
		}

		var item:		Object = {label: this._inputText.text};
		comboBox.addItem( item );

		this._comboBoxIndex++;
		comboBox.selectedItem = item;
		comboBox.selectedIndex = this._comboBoxIndex - 1;
		
		this._comboBox.addEventListener(Event.SELECT, this.handler_select);
		this._comboBox.dispatchEvent( new Event(Event.SELECT) );
	}

	/**
	 * @private
	 */
	private function handler_progress(event:ProgressEvent):void {
		this._label.text = 'Loading ' + event.bytesTotal + ' bytes';

		this._progressBar.maximum = event.bytesTotal;
		this._progressBar.value = event.bytesLoaded;
	}

	/**
	 * @private
	 */
	private function handler_error(event:Event):void {
		var dispatcher:IEventDispatcher = IEventDispatcher(event.target);
		dispatcher.removeEventListener(ProgressEvent.PROGRESS,	this.handler_progress);
		dispatcher.removeEventListener(IOErrorEvent.IO_ERROR,	this.handler_error);

		LoaderInfo(event.target).removeEventListener(Event.COMPLETE,	this.handler_resourceComplete);

		this._label.text = 'Error loading';

		super.stage.removeChild(this._progressBar);

		this._comboBox.removeItem(this._comboBox.selectedItem);
		this._comboBox.selectedItem = null;
		this._comboBoxIndex--;

		this._comboBox.addEventListener(Event.SELECT, this.handler_select);
		this._comboBox.dispatchEvent( new Event(Event.SELECT) );
	}

	/**
	 * @private
	 */
	private function handler_resourceComplete(event:Event):void {
		var loaderInfo:LoaderInfo = LoaderInfo(event.target);
		loaderInfo.removeEventListener(Event.COMPLETE, this.handler_resourceComplete);

		var mc:	MovieClip = MovieClip(this._loader.content);
		this._mc = mc;
		mc.cacheAsBitmap = true;
		mc.x = (super.stage.stageWidth - mc.width) >> 1;
		mc.y = (super.stage.stageHeight - mc.height) >> 1;
		super.addChild(mc);

		super.stage.removeChild(this._progressBar);

		this._comboBox.addEventListener(Event.SELECT, this.handler_select);
	}


}
}